<?php

namespace app\controller;

use app\BaseController;
use think\facade\View;
use app\model\WebLink;
use app\Request;
use think\facade\Db;

class Index extends BaseController
{
  public function index()
  {
    return '<style>*{ padding: 0; margin: 0; }</style><iframe src="https://www.thinkphp.cn/welcome?version=' . \think\facade\App::version() . '" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>';
  }

  public function menu()
  {
    $data = [
      [
        'title' => '数据管理', 'icon' => 'layui-icon-home', 'list' =>
        [
          ['title' => '一层列表', 'icon' => 'layui-icon-home', 'jump' => 'link/info_link'],
          // ['title' => '二层列表', 'icon' => 'layui-icon-home', 'jump' => 'info/info_list'],
          // ['title' => '数据分析', 'icon' => 'layui-icon-senior', 'jump' => 'echarts/line'],
          // ['title' => '长链数据', 'icon' => 'layui-icon-senior', 'jump' => 'excel/index'],
          // ['title' => 'es测试数据', 'icon' => 'layui-icon-senior', 'jump' => 'es/index'],
        ]
      ]
    ];

    return ['code' => 0, 'msg' => 'ok', 'data' => $data];
  }

  public function first_floor_data(Request $request)
  {
    $data = $request->param(); 
    $page = intval(request()->param('page', 1));  // 默认为第1页
    $limit = intval(request()->param('limit', 10));  // 默认每页10条记录
    
    try {
      $query = WebLink::query(); 
      $retData =  $this->getPaginatedData($query, $page, $limit); 
      
      // 返回分页数据
      return json([
        'code' => 0,
        'msg' => 'ok',
        'page' => $page,
        'limit' => $limit,
        'count' => $retData['count'],
        'data' => $retData['data']
      ]);
      
    } catch (\Exception $e) { 
      return response('Error fetching data: ' . $e->getMessage());
    }
  }

  public function search(Request $request){
    $data = $request->param(); 
    $page = intval(request()->param('page', 1));  // 默认为第1页
    $limit = intval(request()->param('limit', 10));  // 默认每页10条记录
    
    try {
      $query = WebLink::query(); 
      $retData =  $this->getPaginatedData($query, $page, $limit); 
      
      // 返回分页数据
      return json([
        'code' => 0,
        'msg' => 'ok',
        'page' => $page,
        'limit' => $limit,
        'count' => $retData['count'],
        'data' => $retData['data']
      ]);
      
    } catch (\Exception $e) { 
      return response('Error fetching data: ' . $e->getMessage());
    }
  }
  

  public function hello($name = 'ThinkPHP8')
  {
    return 'hello,' . $name;
  }
}
